# -*- coding: utf-8 -*-
"""
Created on Wed Jun  5 11:05:02 2019

@author: jlani
"""

import pickle
from PermTester import PermTester
from garp import garp_find_e
import os
import csv
import numpy as np

'''Run the permutation test on the stanford market basket data. 

The input data is:
    price.pickle: time period - good names - price of good
    cons.pickle: participant id - time period - good names - amount consumed.
    
Store results in a file called stanford_perm_test.
There will be four columns. First is participant id, second is time_periods, 
third e-score, fourth is p-value of the test.'''


#Import data
with open('price.pickle','rb') as f:
    price = pickle.load(f)
with open('cons.pickle','rb') as f:
    cons = pickle.load(f)

result_path = 'stanford_perm_rays.csv'
field_names = ['panelist', 'time_periods','e-score','p-value']
#See if results file exists. If not then create it.
if not os.path.isfile(result_path):
    #File doesn't exist so let's create it.
    with open( result_path, 'w', newline='') as f:
        writer = csv.writer( f )
        #Write fieldnames
        writer.writerow( field_names )
        #The file exists now.    

#Iterate over participants in cons.
for panelist in cons.keys():
    #Check to see if the participant has been processed yet.
    skip_panelist = False
    with open( result_path, 'r', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            if str(row[0]) == str(panelist):
                skip_panelist = True
                break
    if skip_panelist: 
        print('already processed ' + str(panelist))
        continue
    #Create RP dataset.
    #Get list of time periods.
    time_periods_list = list( cons[panelist].keys() )
    #Get list of goods.
    goods_list = list( cons[panelist][time_periods_list[0]] )
    
    #Make an empty dataset.
    T = len(time_periods_list)
    L = len(goods_list)
    C = np.zeros( (T,L) )
    P = np.zeros( (T,L) )
    #Populate dataset.
    for t in range(T):
        time_period = time_periods_list[t]
        for ell in range(L):
            good_name = goods_list[ell][2:]
            P[t,ell] = price[time_period]['p.' + good_name]
            C[t,ell] = cons[panelist][time_period]['q.' + good_name]
    e_score = garp_find_e(C,P)
    
    pt = PermTester(C,P)
    p_value = pt('garp','r')
    
    result = [ str(panelist), str(T), str(e_score), str(p_value) ]
    print(field_names)
    print(result)
    
    #Save result.
    #Read the past results
    past_data = []
    with open( result_path, 'r', newline='') as f:
        reader = csv.reader(f)
        for row in reader:
            past_data.append(row)
    
    #Save the result
    print('saving result')
    with open( result_path, 'w', newline='') as f:
        writer = csv.writer(f)
        for row in past_data:
            writer.writerow(row)
        writer.writerow( result )
    







